package com.coment.po.roles;

import com.coment.game.fight.AttributeManagerLogic;
import com.coment.game.fight.Attrs;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Transient;

import java.util.Map;

/// 角色面板
@Slf4j
@Data
@NoArgsConstructor // mongoDB 会使用无参构造器处理
public class RolePanel {
    @JsonIgnore
    public CultivatePanel cultivatePanel;
    @JsonIgnore
    public FightPanel fightPanel;
    @JsonIgnore
    public StatusPanel statusPanel;

    public RolePanel(Integer userInfoId, Integer cardId, Integer level) {
        this.cultivatePanel = new CultivatePanel(userInfoId,cardId, level);
        this.fightPanel = new FightPanel();
        this.statusPanel =  new StatusPanel();
    }

    /// 角色属性(不包含 等级基础属性,天赋属性)
    @Transient
    public Attrs getAttribute() {
        Attrs result = new Attrs();
        if (this.cultivatePanel != null) {
            this.cultivatePanel.getValue().forEach((key, value) -> result.merge(key, value, Double::sum));
        }
        if (this.fightPanel != null) {
            this.fightPanel.getValue().forEach((key, value) -> result.merge(key, value, Double::sum));
        }

        if (this.statusPanel != null) {
            this.statusPanel.getValue().forEach((key, value) -> result.merge(key, value, Double::sum));
        }
        return result;
    }

    /// 变动属性 返回真实变动
    public Attrs obtainUpdateAttrs(Map<Integer, Double> value) {
        Attrs updateValueMap = new Attrs();

        value.forEach((k, addValue) -> {
            try {
                if (k != null) {
                    Double maxValue;
                    Double v = AttributeManagerLogic.ATTR_MAX_VALUE.get(k);
                    if (v == -1) {
                        maxValue = cultivatePanel.getValue().get(k);
                    }else {
                        maxValue = v;
                    }

                    Map<Integer, Double> attribute = this.getAttribute();
                    attribute.merge(k, addValue, Double::sum);
                    Double targetValue = attribute.get(k);

                    if (targetValue > maxValue){
                        targetValue = maxValue;
                    }
                    double updateValue = targetValue - this.getAttribute().get(k);
                    updateValueMap.put(k, updateValue);
                }
            } catch (Exception e) {
                log.error("更新战斗属性出错{}",k, e);
            }
        });
        return updateValueMap;
    }
}
